<?php

	/**
	 * File uploader
	 
	 * @author anton.perkin
	 * @version 1.0
	 */
	class Upload
	{
		/* VARIABLES */
		
		/**
		 * File name
		 *
		 * @var string
		 */
		private $name;

		/**
		 * Uploaded file
		 *
		 * @var string
		 */
		private $uploadedFile;

		/**
		 * Destination folder
		 *
		 * @var string
		 */
		private $destinationFolder;

		/**
		 * New file name
		 *
		 * @var string
		 */
		private $newName;

		/**
		 * Whether to rename the uploaded file
		 *
		 * @var boolean
		 */
		public $rename = true;

		/**
		 * Whether to create thumbnail for the uploaded file
		 *
		 * @var string
		 */
		public $createThumb = true;

		/**
		 * Thumbnail folder
		 *
		 * @var string
		 */
		public $thumbFolder = 'thumbs/';

		/**
		 * Thumbnail width
		 *
		 * @var string
		 */
		public $thumbXsize = 100;

		/**
		 * Thumbnail height
		 *
		 * @var string
		 */
		public $thumbYsize = 100;
		
		
		/**
		 * Default constructor
		 *
		 * @param string $name file name
		 */
		public function __construct($name, $destinationFolder)
		{
			$this->name = $name;
			$this->newName = $name;
			$this->uploadedFile = $_FILES[$this->name];
			$this->destinationFolder = $destinationFolder;
		}
		
		
		/* METHODS */
		
		/**
		 * Uploads the file
		 *
		 * @return boolean
		 */
		public function UploadFile()
		{
			$fileNameComponents = pathinfo($this->uploadedFile["name"]);
			$this->newName = ($this->rename) ? (mktime() + time()).'.'.$fileNameComponents['extension'] : $this->uploadedFile["name"];
			return move_uploaded_file($this->uploadedFile["tmp_name"], $this->destinationFolder.$this->newName);			
		}
		
		/**
		 * Creates thumb image
		 *
		 */
		public function CreateThumb()
		{
			//get the information if GD is at least version 2.0 and check if the original image is a JPEG or PNG.
			$fileParts = explode('.', $this->destinationFolder.$this->newName);
			if (preg_match('/jpg|jpeg/', $fileParts[1]))
			{
				$srcImg = imagecreatefromjpeg($this->destinationFolder.$this->newName);
			}
			else if (preg_match('/png/', $fileParts[1]))
			{
				$srcImg = imagecreatefrompng($this->destinationFolder.$this->newName);
			}
			
			//get the dimensions of the original image by using imageSX() and imageSY(), 
			//and calculate the dimensions of the thumbnail accordingly, keeping the correct aspect ratio. 
			//The desired dimensions are stored in $thumbWidth and $thumbHeight.
			$originalXsize = imageSX($srcImg);
			$originalYsize = imageSY($srcImg);
			if ($originalXsize > $originalYsize) 
			{
				$thumbWidth = $this->thumbXsize;
				$thumbHeight = $originalYsize*($this->thumbYsize/$originalXsize);
			}
			if ($originalXsize < $originalYsize) 
			{
				$thumbWidth = $originalXsize * ($this->thumbXsize/$originalYsize);
				$thumbHeight = $this->thumbYsize;
			}
			if ($originalXsize == $originalYsize)
			{
				$thumbWidth = $this->thumbXsize;
				$thumbHeight = $this->thumbYsize;
			}
			
			//create the image as a true colour version using ImageCreateTrueColor() 
			//and resize and copy the original image into the new thumbnail image, on the top left position.
			$destinationImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);
			imagecopyresampled($destinationImage, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalXsize, $originalYsize);
			if (preg_match("/png/", $fileParts[1]))
			{
				imagepng($destinationImage, $this->destinationFolder.$this->thumbFolder.$this->newName); 
			}
			else
			{
				imagejpeg($destinationImage, $this->destinationFolder.$this->thumbFolder.$this->newName); 
			}
			
			//check the file system extension of the original image and create the thumbnail accordingly. The thumbnail gets saved onto the server (by adding a filename to imagejpeg() or imagepng() function) and the two image objects get destroyed to free the memory.
			imagedestroy($destinationImage); 
			imagedestroy($srcImg); 
		}
		
		/**
		 * Gets uploaded file name
		 *
		 * @return string
		 */
		public function GetUploadedFileName()
		{
			return $this->uploadedFile["name"];
		}
		
		/**
		 * Gets new file name
		 * 
		 * @return string
		 *
		 */
		public function GetNewName()
		{
			return $this->newName;
		}
		
	}

?>